package tongcheng.com.tongcheng.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.request.RequestOptions;

import java.util.List;

import jp.wasabeef.glide.transformations.BlurTransformation;
import tongcheng.com.tongcheng.ConStans;
import tongcheng.com.tongcheng.R;
import tongcheng.com.tongcheng.base.AllOrderDetialActivity;
import tongcheng.com.tongcheng.base.BaseViewHolder;
import tongcheng.com.tongcheng.base.ChooseOrderPeopleActivity;
import tongcheng.com.tongcheng.bean.OrderListBean;
import tongcheng.com.tongcheng.bean.StatusBean;
import tongcheng.com.tongcheng.network.CallServer;
import tongcheng.com.tongcheng.network.MyRequest;
import tongcheng.com.tongcheng.network.OnCallBack;
import tongcheng.com.tongcheng.utils.GlideApp;
import tongcheng.com.tongcheng.utils.GsonUtil;
import tongcheng.com.tongcheng.utils.Setting;
import tongcheng.com.tongcheng.utils.ShowToast;
import tongcheng.com.tongcheng.utils.TimeUtiles;

/**
 * Created by P on 2017/7/27.
 */

public class OrderListAdapter extends RecyclerView.Adapter<OrderListAdapter.OrderViewHolder> {

    public Context mContext;
    public List<OrderListBean.DataBean> datas;
    private boolean aBoolean = false;

    public void setaBoolean(boolean aBoolean) {
        this.aBoolean = aBoolean;
    }


    public OrderListAdapter(Context context, List<OrderListBean.DataBean> datas) {
        mContext = context;
        this.datas = datas;
    }

    @Override
    public OrderViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = View.inflate(mContext, R.layout.item_orderlist, null);
        OrderViewHolder orderViewHolder = new OrderViewHolder(view);

        return orderViewHolder;
    }

    @Override
    public void onBindViewHolder(OrderViewHolder holder, final int position) {
        final OrderListBean.DataBean dataBean = datas.get(position);

        String currenttime = TimeUtiles.getCurrenttime(dataBean.create_time);
        holder.item_order_time.setText(dataBean.create_time_format);
        holder.item_order_area.setText(dataBean.shop_address);
        holder.item_order_price.setText("¥"+dataBean.price);
        holder.item_order_title.setText(dataBean.order_title);

        if (position + 1 == datas.size()) {
            holder.item_line.setVisibility(View.GONE);
        } else {
            holder.item_line.setVisibility(View.VISIBLE);
        }
        Log.e("订单状态", dataBean.status + "");
        switch (dataBean.status) {

            case 0://带抢单
                holder.item_order_state.setText("待抢单");
                holder.item_order_re.setVisibility(View.GONE);
                break;
            case 1://待确认
                holder.item_order_state.setText("待确认");
                holder.item_order_re.setVisibility(View.VISIBLE);
                break;
            case 2://待施工

                holder.item_order_state.setText("待施工");
                holder.item_order_re.setVisibility(View.GONE);
                break;
            case 3://已抢单
                if (Setting.getId() == dataBean.account_id) {
                    holder.item_order_state.setText("待确认开始施工");
                    holder.item_order_re.setVisibility(View.GONE);
                } else {
                    holder.item_order_state.setText("已抢单");
                    holder.item_order_re.setVisibility(View.GONE);
                }

                break;
            case 4://正在施工
                holder.item_order_state.setText("正在施工");
                holder.item_order_re.setVisibility(View.GONE);
                break;
            case 5://施工完成
                holder.item_order_state.setText("施工完成");
                holder.item_order_re.setVisibility(View.VISIBLE);
                break;
            case 6://订单完成
                holder.item_order_state.setText("订单完成");
                holder.item_order_re.setVisibility(View.VISIBLE);
                break;
            case 7://已取消
                holder.item_order_state.setText("已取消");
                holder.item_order_re.setVisibility(View.VISIBLE);
                break;
            case 8:
                if (Setting.getId() == dataBean.account_id) {
                    holder.item_order_state.setText("确认抢单方");
                    holder.item_order_re.setVisibility(View.VISIBLE);
                } else {
                    holder.item_order_state.setText("等待发单方确认");
                    holder.item_order_re.setVisibility(View.VISIBLE);
                }
                break;
        }

       // holder.item_order_icon.setRadius(60,30);
        if (!TextUtils.isEmpty(dataBean.scene_pic)) {
            String[] split = dataBean.scene_pic.split(",");
            RequestOptions options = new RequestOptions();
            RequestOptions.bitmapTransform(new BlurTransformation(mContext, 50));
            options.error(R.mipmap.moren);
            GlideApp.with(mContext).load(ConStans.BaseUrl_Test_url + split[0]).error(R.mipmap.moren).into(holder.item_order_icon);
        } else {
            GlideApp.with(mContext).load(R.mipmap.moren).into(holder.item_order_icon);
        }



        holder.item_order_delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                deleteOrder(dataBean,position);
            }
        });


        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                if ( dataBean.status == 8) {
                    Intent intent = new Intent(mContext, ChooseOrderPeopleActivity.class);
                    intent.putExtra("order_type", dataBean.order_type);
                    intent.putExtra("status", dataBean.status);
                    intent.putExtra("id", dataBean.id);
                    mContext.startActivity(intent);
                } else {
                    Intent intent = new Intent(mContext, AllOrderDetialActivity.class);
                    intent.putExtra("order_type", dataBean.order_type);
                    intent.putExtra("status", dataBean.status);
                    intent.putExtra("id", dataBean.id);
                    if(aBoolean){
                        intent.putExtra("boole",false);
                    }
                    mContext.startActivity(intent);
                }


            }
        });

    }


    //删除订单
    private void deleteOrder(OrderListBean.DataBean dataBean, final int position) {
        MyRequest request = new MyRequest(ConStans.DeleteOrder_Url);
        request.add("id", dataBean.id);
        CallServer.getInstance().addPost(mContext,request, 1, new OnCallBack() {
            @Override
            public void OnSuccess(int what, String json, Object ject) {
                StatusBean statusBean = GsonUtil.GsonToBean(json, StatusBean.class);
                if (statusBean != null) {
                    ShowToast.showToast(statusBean.msg);

                    if (statusBean.status == 1) {
                        datas.remove(position);
                        notifyDataSetChanged();
                    }

                }
            }
        });

    }

    @Override
    public int getItemCount() {
        return datas.size();
    }

    public class OrderViewHolder extends BaseViewHolder{

        public TextView item_order_time,item_order_state,item_order_title,item_order_price,item_order_area;

        public RelativeLayout item_order_re;
        public Button item_order_delete;
        public ImageView item_order_icon;

        public View item_line;

        public OrderViewHolder(View itemView) {
            super(itemView);
        }

        @Override
        public void init(View itemview) {
            item_order_time= itemview.findViewById(R.id.item_order_time);
            item_order_state= itemview.findViewById(R.id.item_order_state);
            item_order_title= itemview.findViewById(R.id.item_order_title);
            item_order_price=    itemview.findViewById(R.id.item_order_price);
            item_order_area= itemview.findViewById(R.id.item_order_area);

            item_order_re= itemview.findViewById(R.id.item_order_re);
            item_order_delete=itemview.findViewById(R.id.item_order_delete);

            item_order_icon= itemview.findViewById(R.id.item_order_icon);

            item_line= itemview.findViewById(R.id.item_line);
        }
    }

}
